package com.androidviewx;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.funx.utils.AppContentUtils;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.xfunc.views.floatsys.FloatWindowManager;

import java.util.List;

/**
 * <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
 *
 *
 */
public class DemoViewSysFloatActivity extends AppCompatActivity {
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_view_sys_float);
    }
    public void addSysFloat(View view) {
        log("点击显示");
        XXPermissions.with(DemoViewSysFloatActivity.this).permission(Manifest.permission.SYSTEM_ALERT_WINDOW)
                         .request(new OnPermissionCallback() {
                             @Override
                             public void onGranted(List<String> permissions, boolean all) {
                                 if(all){
                                     log("全部同意");
                                     FloatWindowManager.show(AppContentUtils.getApp(),R.drawable.ic_edit);
                                 }else {
                                     log("拒绝");
                                     showToast("onGranted");
                                 }
                             }
                             @Override
                             public void onDenied(List<String> permissions, boolean never) {
                                 if (never) {
                                     log("被永久拒绝授权，请手动悬浮窗权限");
                                     showToast("被永久拒绝授权，请手动悬浮窗权限");
                                     // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                     XXPermissions.startPermissionActivity(DemoViewSysFloatActivity.this,permissions);
                                 } else {
                                     log("获取悬浮窗权限失败");
                                     showToast("获取悬浮窗权限失败");
                                 }
                             }
                         });
    }

    public void showToast(String message){
        Toast.makeText(DemoViewSysFloatActivity.this,message,Toast.LENGTH_SHORT).show();
    }
    public void log(String message){
        Log.d("xxx",message);
    }
    public void removeSysFloat(View view) {
        log("点击消失");
        FloatWindowManager.hide();
    }

}
